package com.atguigu.day21.obj04;

import org.junit.Test;

import java.io.*;

/*
对象流:
   序列化: 将内存中的一个对象 存储到磁盘中
   反序列化: 将磁盘中的对象读取到程序内
注意:
   1.NotSerializableException
     序列化对象所在的类必须实现接口 Serializable
   2.某些属性不想被序列化可以在属性前 + transient / static
   3.序列化和反序列化 serialVersionUID 要一致
   4.读写带类型的数据
 */
public class ObjectStreamTest2 {
    @Test
    public void test02(){
        //1.创建对象输入流
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/oos.txt"))) {
            //2.读取数据
            System.out.println("ois.readDouble() = " + ois.readDouble());
            System.out.println("ois.readBoolean() = " + ois.readBoolean());
            System.out.println("ois.readUTF() = " + ois.readUTF());

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }



    }


    @Test
    public void test01(){

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/oos.txt"))) {
            oos.writeDouble(3.14);
            oos.writeBoolean(false);
            oos.writeUTF("千山鸟飞绝");
            System.out.println("写出完毕");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
